import { dateFormat } from 'vux'

class IngotTime {
  getLocalTimeZone() {
    return new Date().getTimezoneOffset() / 60
  }

  transferTimeToLocal(time) {
    const timezone = 0
    const offset = 3600000 * timezone
    const formatDate = dateFormat(time)
    const oldTime = new Date(formatDate)
    const oldLen = oldTime.getTime()
    const utcTime = oldLen + offset
    const newDate = new Date(utcTime - 3600000 * this.getLocalTimeZone())
    return dateFormat(newDate, 'YYYY-MM-DD')
  }

  transferTimeToServer(time) {
    const formatDate = dateFormat(time)
    const localDate = new Date(formatDate)
    //  得到1970年一月一日到现在的秒数
    const local = localDate.getTime()
    //  本地时间与GMT时间的时间偏移差
    const offset = localDate.getTimezoneOffset() * 60000
    const utcTime = local + offset
    const utcDate = new Date(utcTime)
    return dateFormat(utcDate)
  }
}

export default new IngotTime()
